package com.baizhi.service;

import com.baizhi.dao.EmpDao;
import com.baizhi.entity.Dept;
import com.baizhi.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService{

    @Autowired
    private EmpDao empDao;




    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Emp> queryPageEmp(Integer pageNo) {
        int begin = (pageNo-1)*5;
        List<Emp> list = empDao.queryPageEmp(begin);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Integer queryAllTotal() {
        Integer total = empDao.queryAllTotal();
        int totalPage = total/5;
        if (total%5!=0)totalPage++;
        return totalPage;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Emp queryEmp(Integer id) {
        Emp emp =  empDao.queryEmp(id);
        return emp;
    }

    @Override
    public void addEmp(Emp emp, String name) {
        Dept dept = empDao.queryDept(name);
        emp.setDept(dept);
        empDao.addEmp(emp);
    }

    @Override
    public void delEmp(List<Integer> list) {
        for (Integer id : list){
            empDao.delEmp(id);
        }
    }

    @Override
    public void delEmpbyId(Integer id) {
        empDao.delEmp(id);
    }

    @Override
    public void updEmp(Emp emp, String name) {
        Dept dept =  empDao.queryDept(name);
        emp.setDept(dept);
        empDao.updEmp(emp);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Dept> queryAllDept() {
        List<Dept>list = empDao.queryAllDept();
        return list;
    }
}
